﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CanvasWPF
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class CanvasWPF : UserControl
    {

        //Private variables
        private Graphics.WPF graphics;
        private GraphicsFramework.DrawingLibrary drawings;
        private GraphicsFramework.Enum.EnumDrawingCommands command = GraphicsFramework.Enum.EnumDrawingCommands.SELECT;

        #region Events and Delegates

        //Events and Delegates - MouseUpdated
        public delegate void MousePositionUpdatedDelegate(float x, float y);
        public event MousePositionUpdatedDelegate MouseUpdated;

        //Events and Delegates - CommandUpdated
        public delegate void CommandUpdatedDelegate(string command);
        public event CommandUpdatedDelegate CommandUpdated;

        //Events and Delegates - DrawingSelected
        public event GraphicsFramework.DrawingLibrary.DrawingSelectedDelegate DrawingSelected;

        //Events and Delegates - DrawingDeselected
        public event GraphicsFramework.DrawingLibrary.DrawingDeselectedDelegate DrawingDeselected;

        #endregion

        #region Constructors

        public CanvasWPF()
        {
            InitializeComponent();

            this.Focusable = true;

            //Initialize variables
            graphics = new Graphics.WPF(100, 100, 100, new GraphicsFramework.Entity.Point2F(1, 1));
            drawings = new GraphicsFramework.DrawingLibrary(graphics);

            //Add Events
            drawings.RefreshDrawing += new GraphicsFramework.DrawingLibrary.RefreshDrawingDelegate(RefreshDrawing);
            drawings.CommandUpdated += new GraphicsFramework.DrawingLibrary.CommandUpdatedDelegate(CommandChanged);
            drawings.DrawingSelected += new GraphicsFramework.DrawingLibrary.DrawingSelectedDelegate(DrawingSelectedChanged);
            drawings.DrawingDeselected += new GraphicsFramework.DrawingLibrary.DrawingDeselectedDelegate(DrawingDeselectedChanged);

            //Add Grid
            drawings.AddGrid(new GraphicsFramework.Drawing.Grid.FullGrid());
        }

        #endregion

        #region DrawingLibrary Events

        /// <summary>
        /// Activated where there is a change of command in the drawing library.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="command"></param>
        private void CommandChanged(GraphicsFramework.Enum.EnumDrawingCommands type, string command)
        {
            if (CommandUpdated != null)
                CommandUpdated(command);

            this.command = type;

            if (this.command == GraphicsFramework.Enum.EnumDrawingCommands.SELECT)
            {
                this.Cursor = Cursors.Arrow;
            }
            else
            {
                this.Cursor = Cursors.Cross;
            }
        }

        /// <summary>
        /// Activated when the graphics library needs to update the canvas.
        /// </summary>
        public void RefreshDrawing()
        {
            if (drawings != null)
            {
                graphics.SetGraphics(MainCanvas.Children);
                drawings.OnRender();
            }
        }

        /// <summary>
        /// Activated when drawings are selected in the drawing library.
        /// </summary>
        /// <param name="selected"></param>
        private void DrawingSelectedChanged(GraphicsFramework.Drawing.Base[] selected)
        {
            if (DrawingSelected != null)
                DrawingSelected(selected);
        }

        private void DrawingDeselectedChanged()
        {
            if (DrawingDeselected != null)
                DrawingDeselected();
        }

        #endregion

        #region Control Events

        protected override void OnRender(DrawingContext drawingContext)
        {
            graphics.SetGraphics(MainCanvas.Children);
            drawings.OnRender();
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            if (drawings != null)
                drawings.OnResize((int)sizeInfo.NewSize.Width, (int)sizeInfo.NewSize.Height);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            bool control = false, shift = false, alt = false;

            if ((e.Key == Key.LeftCtrl) || (e.Key == Key.RightCtrl))
                control = true;

            if ((e.Key == Key.LeftShift) || (e.Key == Key.RightShift))
                shift = true;

            if ((e.Key == Key.LeftAlt) || (e.Key == Key.RightAlt))
                alt = true;

            drawings.OnKeyDown(graphics.GetKey(e), control, shift, alt);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            bool control = false, shift = false, alt = false;

            if ((e.Key == Key.LeftCtrl) || (e.Key == Key.RightCtrl))
                control = true;

            if ((e.Key == Key.LeftShift) || (e.Key == Key.RightShift))
                shift = true;

            if ((e.Key == Key.LeftAlt) || (e.Key == Key.RightAlt))
                alt = true;

            drawings.OnKeyUp(GraphicsFramework.Enum.EnumKeys.None, control, shift, alt);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            this.Focus();

            Point position = e.GetPosition(MainCanvas);
            drawings.OnMouseDown((int)position.X, (int)position.Y, graphics.GetMouseButton(e));
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            Point position = e.GetPosition(MainCanvas);
            drawings.OnMouseMove((int)position.X, (int)position.Y, graphics.GetMouseButton(e));

            if (MouseUpdated != null)
                MouseUpdated(drawings.WorkingCoordinate.X, drawings.WorkingCoordinate.Y);

        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                //Change the cursor base on the current command.
                if (this.command == GraphicsFramework.Enum.EnumDrawingCommands.SELECT)
                {
                    MainCanvas.Cursor = Cursors.Arrow;
                }
                else if (this.command == GraphicsFramework.Enum.EnumDrawingCommands.PAN)
                {
                    MainCanvas.Cursor = Cursors.Hand;
                }
                else
                {
                    MainCanvas.Cursor = Cursors.Cross;
                }
            }

            Point position = e.GetPosition(MainCanvas);
            drawings.OnMouseUp((int)position.X, (int)position.Y, graphics.GetMouseButton(e));
        }

        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            Point position = e.GetPosition(this);
            drawings.OnMouseWheel((int)position.X, (int)position.Y, e.Delta);
        }

        #endregion

        #region GraphicsInterface Members
        #endregion

        #region Properties
        #endregion

    }
}
